<template>
  <div class="header-layout" >
    <div class="header-base">
      <div class="slogan">「WorkMatch」で、あなたの仕事依頼や求人情報を簡単に掲載できます。</div>
      <div class="header-container">
        <router-link to="/"><img src="/logo.png" class="logo" alt="LOGO"></router-link>
        
        <!-- 桌面端菜单 -->
          <el-menu class="desktop-menu" mode="horizontal" :router="true" :default-active="$route.path"
style="width: 500px; border-bottom: none;  height: 45px; line-height: 30px;">
          <el-menu-item index="/find-work" style="width: 80px; padding: 0 12px; height: 45px; line-height: 30px;">仕事を探す</el-menu-item>
          <el-menu-item v-if="isLoggedIn" index="/user/mypage"style="width: 100px; padding: 0 12px; height: 45px; line-height: 30px;">マイページ</el-menu-item>
          <el-menu-item index="/notes" style="width: 80px; padding: 0 12px; height: 45px; line-height: 30px;">お知らせ</el-menu-item>
          <el-menu-item index="/rules" style="width: 100px; padding: 0 12px; height: 45px; line-height: 30px;">報酬規則</el-menu-item>
        </el-menu>
        
        <!-- 移动端未登录按钮 -->
        <div class="mobile-auth-buttons-header" v-if="!isLoggedIn">
          <el-button class="mobile-header-register-btn" @click="$router.push('/register')">会員登録（無料）</el-button>
          <el-icon class="mobile-header-login-btn" @click="$router.push('/login')">
            <SwitchButton />
          </el-icon>
      
        </div>
        
        <!-- 移动端汉堡菜单按钮 -->
        <div class="mobile-menu-button" @click="drawerVisible = true" >
          <el-icon size="24">
            <Menu />
          </el-icon>
        </div>
        
        <!-- 桌面端认证按钮 -->
        <div class="auth-buttons desktop-auth">
          <template v-if="isLoggedIn">
            <el-dropdown trigger="hover" @command="handleCommand">
              <span class="username-dropdown">
                <span>{{ username }} アカウント</span>
                <el-icon class="dropdown-icon">
                  <ArrowDown />
                </el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="mypage">
                    <el-icon>
                      <User />
                    </el-icon>
                    <span>マイページ</span>
                  </el-dropdown-item>
                  <el-dropdown-item command="bankaccount">
                    <el-icon>
                      <CreditCard />
                    </el-icon>
                    <span>銀行口座</span>
                  </el-dropdown-item>
                  <el-dropdown-item command="workorders">
                    <el-icon>
                      <Document />
                    </el-icon>
                    <span>作業注文</span>
                  </el-dropdown-item>
                  <!-- <el-dropdown-item command="joblist">
                    <el-icon>
                      <List />
                    </el-icon>
                    <span>タスクリスト</span>
                  </el-dropdown-item> -->
                  <el-dropdown-item command="payment">
                    <el-icon>
                      <Money />
                    </el-icon>
                    <span>入金</span>
                  </el-dropdown-item>
                  <el-dropdown-item command="withdrawal">
                    <el-icon>
                      <Wallet />
                    </el-icon>
                    <span>出金</span>
                  </el-dropdown-item>
                  <el-dropdown-item command="transactions">
                    <el-icon>
                      <List />
                    </el-icon>
                    <span>取引履歴</span>
                  </el-dropdown-item>
                  <el-dropdown-item command="profile">
                    <el-icon>
                      <Collection />
                    </el-icon>
                    <span>口座情報</span>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
            <el-button class="logout-button" @click="logout">
              <el-icon class="logout-icon">
                <Switch />
              </el-icon>
              やめる
            </el-button>
          </template>
          <template v-else>
            <el-button
              style="background-color: white; color: #333; height: 40px; width: 80px; padding: 0 16px; border: none;"
              @click="$router.push('/login')">ログイン</el-button>
            <el-button style="background-color: #f26731; color: white; height: 40px; width: 145px; padding: 0 16px;"
              @click="$router.push('/register')">会員登録（無料）</el-button>
          </template>
        </div>
      </div>
    </div>
    
    <!-- 移动端侧边抽屉菜单 -->
    <el-drawer
      v-model="drawerVisible"
      title="WorkMatch"
      direction="rtl"
      size="70%"
      :with-header="true"
      :z-index="9999999"
      :modal="true"
      :append-to-body="true"
      :destroy-on-close="false"
    >
      <div class="mobile-menu-container">
        <!-- 移动端菜单项 -->
        <div class="mobile-menu-items">
          <router-link to="/find-work" @click="drawerVisible = false" class="mobile-menu-item">仕事を探す</router-link>
          <router-link v-if="isLoggedIn" to="/user/mypage" @click="drawerVisible = false" class="mobile-menu-item">マイページ</router-link>
          <router-link v-if="isLoggedIn" to="/notes" @click="drawerVisible = false" class="mobile-menu-item">お知らせ</router-link>
          <router-link to="/rules" @click="drawerVisible = false" class="mobile-menu-item">報酬規則</router-link>
        </div>
        
        <!-- 移动端认证按钮 -->
        <div class="mobile-auth">
          <template v-if="isLoggedIn">
            <div class="mobile-user-info">
              <span class="mobile-username">{{ username }}</span>
            </div>
            <div class="mobile-menu-items">
              <router-link to="/user/mypage" @click="drawerVisible = false" class="mobile-menu-item">マイページ</router-link>
              <router-link to="/user/bankaccount" @click="drawerVisible = false" class="mobile-menu-item">銀行口座</router-link>
              <router-link to="/user/workorders" @click="drawerVisible = false" class="mobile-menu-item">作業注文</router-link>
              <router-link to="/user/joblist" @click="drawerVisible = false" class="mobile-menu-item">タスクリスト</router-link>
              <router-link to="/user/payment" @click="drawerVisible = false" class="mobile-menu-item">入金</router-link>
              <router-link to="/user/withdrawal" @click="drawerVisible = false" class="mobile-menu-item">出金</router-link>
              <router-link to="/user/transactions" @click="drawerVisible = false" class="mobile-menu-item">取引履歴</router-link>
              <router-link to="/user/profile" @click="drawerVisible = false" class="mobile-menu-item">口座情報</router-link>
            </div>
            <el-button class="mobile-logout-button" @click="logout">やめる</el-button>
          </template>
          <template v-else>
            <!-- <div class="mobile-auth-buttons">
              <el-button style="width: 100%;" @click="$router.push('/login'); drawerVisible = false">登録</el-button>
              <el-button style="width: 100%;" type="primary" @click="$router.push('/register'); drawerVisible = false">登録する（無料）</el-button>
            </div> -->
          </template>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import {
  User,
  Document,
  CreditCard,
  Money,
  Wallet,
  List,
  Setting,
  Close,
  ArrowDown,
  Switch,
  Menu,
  Collection,
} from '@element-plus/icons-vue'

// 移动端抽屉菜单状态
const drawerVisible = ref(false)

const router = useRouter()
const isLoggedIn = ref(false)
const username = ref('')

// 检查用户是否已登录
const checkLoginStatus = () => {
  const userInfoStr = localStorage.getItem('userInfo')

  if (userInfoStr) {
    try {
      const userInfo = JSON.parse(userInfoStr)
      console.log(userInfo)
      if (userInfo.type === 2) {
        username.value = userInfo.company_name 
      } else {
        username.value = userInfo.username || userInfo.email 
      }
      isLoggedIn.value = true
    } catch (error) {
      console.error('解析用户信息失败:', error)
      isLoggedIn.value = false
    }
  } else {
    isLoggedIn.value = false
  }
}

// 处理下拉菜单命令
const handleCommand = (command) => {
  if (command === 'logout') {
    logout()
  } else {
    // 导航到对应的用户页面
    router.push(`/user/${command}`)
  }
}

// 退出登录
const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userInfo')
  localStorage.removeItem('accountInfo')
  isLoggedIn.value = false
  username.value = ''
  router.push('/login')
}

// 组件挂载时检查登录状态
onMounted(() => {
  checkLoginStatus()
  // 监听存储变化，实时更新登录状态
  window.addEventListener('storage', checkLoginStatus)
})
</script>

<style scoped>
.el-menu--horizontal .el-menu-item {
  --el-menu-active-color: #409EFF;
  --el-menu-hover-text-color: #409EFF;
  --el-menu-hover-bg-color: transparent;
  position: relative;
}

.el-menu--horizontal .el-menu-item:after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 70%;  /* 控制下划线宽度 */
  height: 6px;
  background-color: #1961a8;
  transition: all 0.25s;
  opacity: 0;
}

.el-menu--horizontal .el-menu-item:hover:after {
  opacity: 1;
}
.logout-button {
  background-color: #0086e6;
  color: #ffffff;
  height: 32px;
  padding: 0 12px;
  border: 1px solid #0086e6;
  border-radius: 4px;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  gap: 5px;
}

.logout-button:hover {
  background-color: #169bfa;
  color: white;
  border-color: #169bfa;
}

.logout-icon {
  margin-right: 5px;
  font-size: 14px;
}

.slogan {
  width: 100%;
  text-align: left;
  font-size: 10px;
  color: #666;
  margin-top: 8px;
}

.header-layout {
  display: flex;
  flex-direction: column;
  max-width: 100%;
  height: 74px;
  border-bottom: 1px solid #d4d4d4;
  box-shadow: 0 0 4px 0 rgba(0, 0, 0, .3), 0 0 12px 6px rgba(0, 0, 0, .15);
  position: relative;
z-index: 1;
}

.header-base {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  max-width: 1200px;
  width: 100%;
  padding: 0 20px;
  margin-left: 310px;
  /* margin-right: 100px; */
  box-sizing: border-box;
}

.header-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.logo {
  height: 35px;

}

.auth-buttons {
  margin-left: 24px;
  display: flex;
  align-items: center;
}

.username-dropdown {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  cursor: pointer;
  min-width: 120px;
  height: 32px;
  padding: 0 12px;
  border-radius: 4px;
  transition: all 0.3s;
  font-weight: bold;
  color: #fff;
  font-size: 14px;
  background-color: #0086e6;
  margin-right: 10px;
  border: none;
}

.username-dropdown:hover {
  opacity: 0.9;
  background-color: #169bfa;
}

.dropdown-icon {
  margin-left: 5px;
  font-size: 12px;
  color: #ffffff;
}

.el-dropdown-menu {
  min-width: 150px;
}

.el-dropdown-item {
  display: flex;
  align-items: center;
  padding: 10px 15px;
}

.el-dropdown-item .el-icon {
  margin-right: 10px;
  font-size: 14px;
}

.el-dropdown-item:hover {
  background-color: #f5f7fa;
}

.el-dropdown-item.is-disabled {
  color: #c0c4cc;
  cursor: not-allowed;
}

.el-menu-item {
  border-bottom: none !important;
  background-color: transparent !important;
  position: relative;
  margin-left: 12px;
  font-size: 12px;
  box-sizing: border-box;
  white-space: nowrap;
  overflow: visible;
}

.el-menu-item.is-active {
  border-bottom: none !important;
  color: #333 !important;
  background-color: transparent !important;
}

.el-menu--horizontal>.el-menu-item.is-active {
  color: #333 !important;
  border-bottom: none !important;
}

.el-menu-item::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0;
  height: 2px;
  background-color: var(--el-color-primary);
  transition: all 0.2s ease;
  transform: translateX(-50%);
}

.el-menu-item:hover::after {
  width: 100%;
}

/* 移动端头部认证按钮 */
.mobile-auth-buttons-header {
  display: none;
  gap: 8px;
  margin-left: auto;
  margin-right: 8px;
}

.mobile-header-login-btn {
  background-color: white;
  color: #333;
  height: 30px;
  padding: 0 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 11px;
  min-width: 50px;
  white-space: nowrap;
}

.mobile-header-register-btn {
  background-color: #f26731;
  color: white;
  height: 30px;
  padding: 0 8px;
  border: none;
  border-radius: 4px;
  font-size: 11px;
  min-width: 70px;
  white-space: nowrap;
}

/* 移动端菜单按钮 */
.mobile-menu-button {
  display: none;
  cursor: pointer;
  padding: 8px;
  margin-left: auto;
}

/* 移动端菜单容器 */
.mobile-menu-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* 移动端菜单项 */
.mobile-menu-items {
  display: flex;
  flex-direction: column;
  margin-bottom: 2rem;
}

.mobile-menu-item {
  padding: 15px 0;
  font-size: 14px;
  color: #333;
  text-decoration: none;
  border-bottom: 1px solid #eee;
  display: block;
}

.mobile-menu-item:hover {
  color: #0086e6;
}

/* 移动端用户信息 */
.mobile-user-info {
  padding: 10px 0;
  /* margin-bottom: 15px; */
  border-bottom: 1px solid #eee;
}

.mobile-username {
  font-size: 16px;
  font-weight: bold;
  color: #0086e6;
}

/* 移动端认证按钮 */
.mobile-auth {
  margin-top: auto;
  padding-top: 20px;
}

.mobile-auth-buttons {
  display: flex;
  flex-direction: column;
  gap: 15px;
  width: 100%;
}

.mobile-auth-buttons .el-button {
  margin-left: 0;
}

.mobile-logout-button {
  width: 100%;
  /* margin-top: 20px; */
  margin-bottom: 20px;
  background-color: #0086e6;
  color: white;
}

/* 响应式设计 */
@media screen and (max-width: 1200px) {
  .header-base {
    margin-left: 50px;
    margin-right: 50px;
  }
}

@media screen and (max-width: 992px) {
  .header-base {
    margin-left: 20px;
    margin-right: 20px;
  }
}

@media screen and (max-width: 768px) {
  .desktop-menu,
  .desktop-auth {
    display: none;
  }
  
  .header-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    gap: 10px;
  }
  
  .logo {
    flex-shrink: 0;
  }
  
  .mobile-auth-buttons-header {
    display: flex;
    align-items: center;
    flex-shrink: 0;
  }
  
  .mobile-menu-button {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }
  
  .header-base {
    margin-left: -5px;
    padding: 0 10px;
  }
  
  .header-layout {
    z-index: 1;
  }
}
</style>